<template>
  <view>
    <button @click="playSound">播放声音</button>
  </view>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue';

const innerAudioContext = ref(null);

onMounted(() => {
  innerAudioContext.value = uni.createInnerAudioContext();
  innerAudioContext.value.src = '/static/audio/xm3463.mp3';
  innerAudioContext.value.autoplay = false;

  innerAudioContext.value.onEnded(() => {
    console.log('音频播放完毕');
  });
});

const playSound = () => {
  innerAudioContext.value.play();
};

onBeforeUnmount(() => {
  if (innerAudioContext.value) {
    innerAudioContext.value.stop();
    innerAudioContext.value.destroy();
    innerAudioContext.value = null;
  }
});
</script>
